package com.yanqu.road.dao.impl.activity.flowerstudy;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.flowerstudy.config.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FlowerStudyConfigDaoImpl extends TempDao {

    private static String getFlowerStudySessionRewardConfigMapSql = "select * from `t_s_activity_studyroom_reward` where `activityId`=? ORDER BY `type` ASC";
    public Map<Integer,Map<Integer,FlowerStudySessionRewardConfig>> getFlowerStudySessionRewardConfigMap(int activityId) {
        Map<Integer,Map<Integer,FlowerStudySessionRewardConfig>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerStudySessionRewardConfigMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerStudySessionRewardConfig flowerStudySessionRewardConfig = new FlowerStudySessionRewardConfig();
                flowerStudySessionRewardConfig.setActivityId(rs.getInt("activityId"));
                flowerStudySessionRewardConfig.setType(rs.getInt("type"));
                flowerStudySessionRewardConfig.setRightNum(rs.getInt("rightNum"));
                flowerStudySessionRewardConfig.setReward(rs.getString("reward"));
                map.putIfAbsent(flowerStudySessionRewardConfig.getType(),new ConcurrentHashMap<>());
                map.get(flowerStudySessionRewardConfig.getType()).put(flowerStudySessionRewardConfig.getRightNum(),flowerStudySessionRewardConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFlowerStudyExamQuestionConfigSql = "select * from `t_s_activity_studyroom_question` where `activityId`=?";
    public Map<Integer,FlowerStudyExamQuestionConfig> getFlowerStudyExamQuestionConfigMap(int activityId) {
        Map<Integer,FlowerStudyExamQuestionConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyExamQuestionConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerStudyExamQuestionConfig flowerStudyExamQuestionConfig = new FlowerStudyExamQuestionConfig();
                flowerStudyExamQuestionConfig.setActivityId(rs.getInt("activityId"));
                flowerStudyExamQuestionConfig.setId(rs.getInt("id"));
                flowerStudyExamQuestionConfig.setQuestionDesc(rs.getString("questionDesc"));
                flowerStudyExamQuestionConfig.setAnswer(rs.getInt("answer"));
                map.put(flowerStudyExamQuestionConfig.getId(),flowerStudyExamQuestionConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }


    private static String getFlowerStudyExamConfigSql = "select * from `t_s_activity_studyroom_exam` where `activityId`=? ORDER BY `id` ASC";
    public Map<Integer,FlowerStudyExamConfig> getFlowerStudyExamConfigMap(int activityId) {
        Map<Integer,FlowerStudyExamConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyExamConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerStudyExamConfig flowerStudyExamConfig = new FlowerStudyExamConfig();
                flowerStudyExamConfig.setActivityId(rs.getInt("activityId"));
                flowerStudyExamConfig.setId(rs.getInt("id"));
                flowerStudyExamConfig.setType(rs.getInt("type"));
                flowerStudyExamConfig.setName(rs.getString("name"));
                flowerStudyExamConfig.setSession(rs.getString("session"));
                flowerStudyExamConfig.setStartTime(rs.getString("startTime"));
                flowerStudyExamConfig.setEndTime(rs.getString("endTime"));
                map.put(flowerStudyExamConfig.getId(),flowerStudyExamConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFlowerStudyPoemConfigSql = "select * from `t_s_activity_studyroom_poem` where `activityId`=? ORDER BY `id` ASC";
    public Map<Integer,FlowerStudyPoemConfig> getFlowerStudyPoemConfigMap(int activityId) {
        Map<Integer,FlowerStudyPoemConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyPoemConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerStudyPoemConfig flowerStudyPoemConfig = new FlowerStudyPoemConfig();
                flowerStudyPoemConfig.setActivityId(rs.getInt("activityId"));
                flowerStudyPoemConfig.setId(rs.getInt("id"));
                flowerStudyPoemConfig.setDesc(rs.getString("desc"));
                flowerStudyPoemConfig.setExtendParam(rs.getString("extendParam"));
                map.put(flowerStudyPoemConfig.getId(),flowerStudyPoemConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    private static String getFlowerStudyTitleConfigSql = "select * from `t_s_activity_studyroom_title` where `activityId`=? ORDER BY `id` ASC";
    public List<FlowerStudyTitleConfig> getFlowerStudyTitleConfigList(int activityId) {
        List<FlowerStudyTitleConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyTitleConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerStudyTitleConfig flowerStudyTitleConfig = new FlowerStudyTitleConfig();
                flowerStudyTitleConfig.setActivityId(rs.getInt("activityId"));
                flowerStudyTitleConfig.setId(rs.getInt("id"));
                flowerStudyTitleConfig.setType(rs.getInt("type"));
                flowerStudyTitleConfig.setRank(rs.getInt("rank"));
                flowerStudyTitleConfig.setExamTypeName(rs.getString("examTypeName"));
                flowerStudyTitleConfig.setName(rs.getString("name"));
                list.add(flowerStudyTitleConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getFlowerStudyVisitConfigSql = "select * from `t_s_activity_studyroom_visit` where `activityId`=? ORDER BY `id` ASC";
    public Map<Integer,FlowerStudyVisitConfig> getFlowerStudyVisitConfigMap(int activityId) {
        Map<Integer,FlowerStudyVisitConfig> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyVisitConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                FlowerStudyVisitConfig flowerStudyVisitConfig = new FlowerStudyVisitConfig();
                flowerStudyVisitConfig.setActivityId(rs.getInt("activityId"));
                flowerStudyVisitConfig.setId(rs.getInt("id"));
                flowerStudyVisitConfig.setDesc(rs.getString("desc"));
                flowerStudyVisitConfig.setReward(rs.getString("reward"));
                flowerStudyVisitConfig.setWeight(rs.getInt("weight"));
                map.put(flowerStudyVisitConfig.getId(),flowerStudyVisitConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }



}
